package com.kinggrid.consolemanager.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.kinggrid.consolemanager.config.R;
import com.kinggrid.consolemanager.entity.SysProject;
import com.kinggrid.consolemanager.entity.SysTag;
import com.kinggrid.consolemanager.service.SysProjectService;
import com.kinggrid.consolemanager.service.SysTagService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author wuth
 * @date 2021-6-16
 */
@RestController
public class IndexController {

    @Autowired
    private SysProjectService sysProjectService;

    @Autowired
    private SysTagService sysTagService;

    @GetMapping(value = "/tag/list")
    public R<List<SysTag>> getTag() {
        return R.ok(sysTagService.list());
    }

    @PostMapping("/tag/add")
    public R addTag(@RequestBody SysTag sysTag) {
        return sysTagService.save(sysTag) ? R.ok() : R.failed();
    }

    @PutMapping("/tag/update")
    public R updateTag(@RequestBody SysTag sysTag) {
        return sysTagService.updateById(sysTag) ? R.ok() : R.failed();
    }

    @DeleteMapping("/tag/del/{id}")
    public R delTag(@PathVariable(value = "id") Long id) {
        return sysTagService.removeById(id) ? R.ok() : R.failed();
    }


    @GetMapping(value = "/project/list")
    public R<List<SysProject>> getProject(SysProject sysProject) {
        LambdaQueryWrapper<SysProject> queryWrapper = Wrappers.<SysProject>lambdaQuery();
        if (null != sysProject && null != sysProject.getTagId()) {
            queryWrapper.eq(SysProject::getTagId, sysProject.getTagId());
        }
        queryWrapper.orderByDesc(SysProject::getId);
        return R.ok(sysProjectService.list(queryWrapper));
    }

    @PostMapping("/project/add")
    public R addProject(@RequestBody SysProject sysTag) {
        return sysProjectService.save(sysTag) ? R.ok() : R.failed();
    }

    @PutMapping("/project/update")
    public R updateProject(@RequestBody SysProject sysTag) {
        return sysProjectService.updateById(sysTag) ? R.ok() : R.failed();
    }

    @DeleteMapping("/project/del/{id}")
    public R delProject(@PathVariable(value = "id") Long id) {
        return sysProjectService.removeById(id) ? R.ok() : R.failed();
    }


}
